#include<iostream>
using namespace std;
#include<vector>
class Solution {
public:
    int numRookCaptures(vector<vector<char>>& board) {
        int dx[4] = { -1,1,0,0 }, dy[4] = { 0,0,-1,1 };
        int nowx, nowy;
        int m = board.size(), n = board[0].size();
        for (int i = 0; i < m; ++i) {
            int flag = 0;
            for (int j = 0; j < n; ++j) {
                if (board[i][j] == 'R') {
                    nowx = i;
                    nowy = j;
                    flag = 1;
                    break;
                }
            }
            if (flag)
                break;
        }
        int sum = 0;
        for (int i = 0; i < 4; ++i) {
            int x = nowx, y = nowy;
            while (1) {
                x += dx[i];
                y += dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n) {
                    if (board[x][y] == '.')
                        continue;
                    if (board[x][y] == 'p')
                        ++sum;
                    break;
                }
                else
                    break;
            }
        }
        return sum;
    }
};